<?php
/**
 *
 * @author Tongle Xu <xutongle@gmail.com> 2013-9-14
 * @copyright Copyright (c) 2003-2103 tintsoft.com
 * @license http://www.tintsoft.com
 * @version $Id$
 */
class Web_Application extends Base_Application {

	/**
	 * (non-PHPdoc)
	 *
	 * @see Base_Application::processRequest()
	 */
	public function processRequest() {
		$route = $this->getRouter ()->parseUrl ( Kernel::getInstance('HttpRequest') );
		// echo $this->getRouter ()->createUrl('site/main',array('aaa'=>'bbb')
		// );
		print_r ( $_GET );
		exit ();
		$this->runController ( $route );
	}

	/**
	 * 创建控制器
	 *
	 * @param unknown $route
	 * @throws HttpException
	 */
	public function runController($route) {
		if (($ca = $this->createController ( $route )) !== null) {
			list ( $controller, $actionID ) = $ca;
			$oldController = $this->_controller;
			$this->_controller = $controller;
			$controller->init ();
			$controller->run ( $actionID );
			$this->_controller = $oldController;
		} else
			throw new HttpException ( 404, Yii::t ( 'yii', 'Unable to resolve the request "{route}".', array ('{route}' => $route === '' ? $this->defaultController : $route ) ) );
	}

	/**
	 * 创建控制器
	 *
	 * @param unknown $route
	 * @param string $owner
	 * @return NULL multitype:NULL <NULL, multitype:NULL , multitype:unknown
	 *         NULL >|multitype:unknown NULL
	 */
	public function createController($route, $owner = null) {
		if ($owner === null) $owner = $this;
		if (($route = trim ( $route, '/' )) === '') $route = $owner->defaultController;
		$caseSensitive = $this->getUrlManager ()->caseSensitive;

		$route .= '/';
		while ( ($pos = strpos ( $route, '/' )) !== false ) {
			$id = substr ( $route, 0, $pos );
			if (! preg_match ( '/^\w+$/', $id )) return null;
			if (! $caseSensitive) $id = strtolower ( $id );
			$route = ( string ) substr ( $route, $pos + 1 );
			if (! isset ( $basePath )) {
				if (isset ( $owner->controllerMap [$id] )) {
					return array (Yii::createComponent ( $owner->controllerMap [$id], $id, $owner === $this ? null : $owner ),$this->parseActionParams ( $route ) );
				}
				if (($module = $owner->getModule ( $id )) !== null) return $this->createController ( $route, $module );
				$basePath = $owner->getControllerPath ();
				$controllerID = '';
			} else
				$controllerID .= '/';
			$className = ucfirst ( $id ) . 'Controller';
			$classFile = $basePath . DIRECTORY_SEPARATOR . $className . '.php';

			if ($owner->controllerNamespace !== null) $className = $owner->controllerNamespace . '\\' . $className;

			if (is_file ( $classFile )) {
				if (! class_exists ( $className, false )) require ($classFile);
				if (class_exists ( $className, false ) && is_subclass_of ( $className, 'CController' )) {
					$id [0] = strtolower ( $id [0] );
					return array (new $className ( $controllerID . $id, $owner === $this ? null : $owner ),$this->parseActionParams ( $route ) );
				}
				return null;
			}
			$controllerID .= $id;
			$basePath .= DIRECTORY_SEPARATOR . $id;
		}
	}

	/**
	 * 初始化
	 */
	protected function init() {
	}
	public function afterControllerAction($controller, $action) {
	}
	public function beforeControllerAction($controller, $action) {
		return true;
	}
}